package com.ld.security.common.tomcat.base.api;


import cn.hutool.http.HttpUtil;
import com.ld.security.common.core.base.ESuperEntity;
import com.ld.security.common.core.util.FileUtil;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.common.mybatis.base.po.*;
import com.ld.security.common.mybatis.base.service.ISuperService;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.mybatis.base.vo.EntityVo;
import com.ld.security.common.mybatis.base.vo.ListVo;
import com.ld.security.common.mybatis.base.vo.PageVo;
import com.ld.security.common.mybatis.util.SimpExcelUtil;
import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.common.tomcat.util.RespUtil;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.io.File;
import java.util.List;

/**
 * 基本控制器
 *
 * @param <E>
 */

public abstract class SuperApi<E extends ESuperEntity> {

    @Autowired
    private ISuperService<E> iSuperService;



    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("修改数据")
@AjaxLogKey
    public DataVo save(@Valid @RequestBody EntityPo<E> po) {

        E entity = po.getEntity();


        int total = iSuperService.save(entity);

        DataVo dataVo =  DataVo.of(po);
        dataVo.setMessage("成功修改"+total+"条数据");

        return dataVo;
    }

/*

    @RequestMapping(value = "/saveBatch", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("批量修改数据")
    public DataVo saveBatch(@Valid @RequestBody EntitysPo<E> po) {
        List<E> entitys = po.getList();

        int total = iSuperService.saveBatch(entitys);


        DataVo vo =   new DataVo<>();
        vo.setData(po);
        vo.setMessage("成功修改"+total+"条数据");
        return vo;
    }
*/

//
//    @RequestMapping(value = "/newEntity", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation("创建实体")
//    public DataVo newEntity(@RequestBody EntityPo<E> po) {
//        E entity =  po.getEntity();
//        entity.setId(UuidUtil.getId());
//
//        return DataVo.of(entity);
//    }

    @AjaxLogKey
    @RequestMapping(value = "/removeByIds", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("批量删除数据")
    public DataVo removeByIds( @Valid @RequestBody IdsPo po) {
        int total = iSuperService.removeByIds(po.getIds());
        DataVo vo =   new DataVo<>();
        vo.setMessage("成功删除"+total+"条数据");
        return vo;
    }


    //    @RequestMapping(value = "/all", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation("查询所有数据")
//    public DataVo<PageVo<E>> all(@RequestBody EntityPo<E> po) {
//
//        List<E> page = iSuperService.all(po.getEntity());
//        PageVo vo = PageVo.of(page);
//        DataVo dataVo =   DataVo.of(vo);
//        return dataVo;
//    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("根据编号查询")
    public DataVo<E> getById( @RequestBody IdPo po) {
        E e = iSuperService.getById(po.getId());
        if(e == null){
            e = this.newInstance();
        }
        return DataVo.of(e);
    }


    @AjaxLogKey
        @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("分页查询数据")
    public DataVo<PageVo<E>> page(@Valid @RequestBody PagePo<E> po) {
        if(po.getQuery() == null){
            po.setQuery(this.newInstance());
        }
        PageVo<E> page = iSuperService.page(po);
//        for (E e : page.getList()) {
//            CrptUtil.decrpt(e);
//        }
        PageVo vo = page;
        DataVo dataVo =   DataVo.of(vo);
        return dataVo;
    }


    @ApiOperation("查询所有")
    @ResponseBody
    @RequestMapping(value = "/all",method = RequestMethod.POST)
    public DataVo<PageVo<E>> all(@Valid @RequestBody ListPo<E> po) {
        List<E> page = iSuperService.all(po);
        ListVo vo =  ListVo.of(page);
        DataVo dataVo =   DataVo.of(vo);
        dataVo.setMessage("");
        return dataVo;
    }


    @AjaxLogKey
    @ApiOperation("依赖查询")
    @ResponseBody
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    public DataVo<PageVo<E>> select(@Valid @RequestBody PagePo<E> po) {
        if(po.getQuery() == null){
            po.setQuery(this.newInstance());
        }
        PageVo<E> page = iSuperService.page(po);
        PageVo vo = page;
        DataVo dataVo =   DataVo.of(vo);
        dataVo.setMessage("");
        return dataVo;
    }



    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("导出excel")
    @SneakyThrows
    public void exportExcel(IdsPo po) {
        E e = this.newInstance();
        List<E> page = iSuperService.all(po);

        byte[] bs = SimpExcelUtil.getEntityWriter(this.getEntityClass(),page);

        RespUtil.downloadFile(this.getEntityClass().getSimpleName()+ ".xls",bs);
    }


    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("导入excel")
    @SneakyThrows
    public DataVo importExcell(  @RequestBody IdsPo po) {
        int total=0;
        for (String s : po.getIds()) {
            String suffix = FileUtil.getSuffix(s);
            File f = new File(UuidUtil.getId(s)+"."+suffix);
            HttpUtil.downloadFile(s, f);
            List<E> list = SimpExcelUtil.getEntityReader(this.getEntityClass(),f);
            iSuperService.saveBatch(list);
            total+=list.size();
            //文件删除
            f.delete();
        }
        DataVo vo =DataVo.of();
        vo.setMessage("成功导入"+total+"条数据");
        return vo;
    }


//
//
//    @RequestMapping(value = "/importExcell", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation("导入excell")
//    @SneakyThrows
//
//    public PageVo importExcell(MultipartFile multipartFile) {
//
//        if (multipartFile.isEmpty()) {
//            throw new RuntimeException("文件不存在");
//        }
//        InputStream inputStream =  multipartFile.getInputStream();
//
//        List<E> list = SimpExcelUtil.getReader(this.getEntityClass(),inputStream);
//
//        PageVo vo = PageVo.of(list);
//
//        return vo;
//    }




    /**
     * 得到泛型
     *
     * @return
     */
    public Class<E> getEntityClass() {
        return iSuperService.getEntityClass();
    }

    /**
     * 得到泛型实例
     *
     * @return
     */
    protected E newInstance() {
        return iSuperService.newInstance();
    }

    public ISuperService<E> getiSuperService() {
        return iSuperService;
    }
}
